package com.example.androidtvdemo.net;


import com.example.androidtvdemo.BuildConfig;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import okhttp3.OkHttpClient;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class HttpManager {

    private static boolean showLog = BuildConfig.DEBUG;
    private static boolean useUnsafeHttps = !BuildConfig.DEBUG;
    private static final HashMap<String, Object> SERVICE_MAP = new HashMap<>();
    public static String[] sslCertPath;
    private static Converter.Factory fastJsonConverterFactory = GsonConverterFactory.create();

    private static final int DEFAULT_TIMEOUT = 10;

    private static final int DEFAULT_UPLOAD_TIMEOUT = 60;

    private static <T> T createService(Class<T> serviceClass, String baseUrl) {
        Retrofit mRetrofitClient = generateRetrofitClient(baseUrl);
        return mRetrofitClient.create(serviceClass);
    }

    public synchronized static <T> T getService(Class<T> serviceClass, String baseUrl) {
        //缓存
        if (SERVICE_MAP.containsKey(serviceClass.getName())) {
            return (T) SERVICE_MAP.get(serviceClass.getName());
        } else {
            Object obj = createService(serviceClass, baseUrl);
            SERVICE_MAP.put(serviceClass.getName(), obj);
            return (T) obj;
        }
    }



    public static Retrofit generateRetrofitClient(String baseUrl) {
        //Client配置
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        if (useUnsafeHttps) {
            builder.sslSocketFactory(getSSLSocketFactory());
            builder.hostnameVerifier(getHostnameVerifier());
        } else if (sslCertPath != null) {
        }
        builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        builder.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        builder.writeTimeout(DEFAULT_UPLOAD_TIMEOUT, TimeUnit.SECONDS);

        builder.retryOnConnectionFailure(true);
        OkHttpClient mApiClient = builder.build();

        return new Retrofit.Builder()
                .client(mApiClient)
                .baseUrl(baseUrl)
                .addConverterFactory(fastJsonConverterFactory)
                .build();
    }


    private static SSLSocketFactory getSSLSocketFactory() {
        try {
            SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, getTrustManager(), new SecureRandom());
            return sslContext.getSocketFactory();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static TrustManager[] getTrustManager() {
        TrustManager[] trustAllCerts = new TrustManager[]{
                new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(X509Certificate[] chain, String authType) {
                    }

                    @Override
                    public void checkServerTrusted(X509Certificate[] chain, String authType) {
                    }

                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[]{};
                    }
                }
        };
        return trustAllCerts;
    }

    private static HostnameVerifier getHostnameVerifier() {
        HostnameVerifier hostnameVerifier = new HostnameVerifier() {
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        };
        return hostnameVerifier;
    }

    private static KeyManager[] prepareKeyManager(InputStream bksFile, String password) {
        try {
            if (bksFile == null || password == null) {
                return null;
            }

            KeyStore clientKeyStore = KeyStore.getInstance("BKS");
            clientKeyStore.load(bksFile, password.toCharArray());
            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            keyManagerFactory.init(clientKeyStore, password.toCharArray());
            return keyManagerFactory.getKeyManagers();

        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnrecoverableKeyException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



}
